<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	
 	public $cacheAction = '1 hour';
 	
 	public $links = null;
	
	public $components = array(
	        'Acl',
	        'Auth' => array('authorize' => array( 'Actions' => array('actionPath' => 'controllers'))),
	        'Session',
	        'Menu.Menu',
	        'Article.Articles',
			'Configuration.Configuration',
			'FileUpload.FileUpload',
			'Password',
			'Templates'
			
	);

	
	public $helpers = array('Tree','Html', 'Form', 'Session','Js','Article.Articles','FileUpload.FileUpload','Menu.Menu','Homepage','Time','Text','Cache');
	

	
	public function beforeFilter() {
		parent::beforeFilter();
		// load configuration form DB
		$this->loadModel('Domain');
		$this->Domain->recursive = -1;
		$domain = $this->Domain->get_info(Configure::read('Config.domain'));
		if(isset($domain['Domain']['id'])){
			$this->Session->write('Config.domain_id',$domain['Domain']['id']);
			$this->Session->write('Config.Domain',$domain);
			Configure::write('Config.domain.id',$domain['Domain']['id']);
			$this->Configuration->load($domain['Domain']['id']);
			
			
			
		}else{
			
		}
		
		Configure::write('debug',Configure::read('cfg.debuging'));
		
		$this->_setLanguage();
		
		//Configure AuthComponent
		$this->Auth->allow('display');
		
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
		$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
		$this->Auth->loginRedirect = array('controller' => 'menuses', 'action' => 'index');
		$this->Auth->loginError = __d('global',"Your username and/or password was not recognized. Use the form below in order to retrieve your login details.",true);
		$this->Auth->authError = __d('global',"You don't have permission to access this page!",true);
		$this->Session->write('Config.domain',Configure::read('Config.domain'));	


// 		$this->Auth->authorize = array(
// 			AuthComponent::ALL => array('actionPath' => 'controllers/'),
// 			'Actions',
// 			'Controller'
// 		);
		
		$path = func_get_args();
		// slugs from menuses
		$this->links = $this->Menu->getMenu($path);
		
		$this->theme = 'Czudec';
	}
	
	public function beforeRender(){
		parent::beforeRender();
		$this->theme = 'Czudec';
	}
	
	/**
	 * 
	 * set language
	 */
	public function _setLanguage() {
		$this->locale = Configure::read('cfg.language');
		Configure::write('Config.language', $this->locale);
		$this->Session->write('Config.language', $this->locale);
	}
	

	
	/**
	*
	* Przekształcenie struktury Tree do wyświetlaneia w combobox lub na liscie
	* @param array $data // dane z zapytania $model->find('treahend');
	* @param string $model // nazwa modeluz którego pobrano dane
	* @param bool $full // true: zwraca array dodając jedynie wartosc depth; false: zwaraca listę id => name;
	*/
	protected function treeForCombobox($data,$model,$display=array('model'=>null,'name'=>null),$full=false){
		$parentCategories_ = $this->treeComboBox($data,$model,$display);
		if($full) return $parentCategories_;
		$parentCategories = array();
		foreach ($parentCategories_ as $key => $value) {
			$parentCategories[$value[$model]['id']] = $value[$display['model']][$display['name']];
		}
		return $parentCategories;
	}
	
	
	/**
	 *
	 * Dodaje wartość depth
	 * @param $data
	 * @param $model
	 */
	private function treeComboBox($data,$model,$display) {
		$result = array();
		foreach ($data as $i) {
			$newData = $i;
			$newData[$model]['depth'] = count($this->{$model}->getPath($i[$model]['id'])) - 1;
			$newData[$model][$display['name']] = str_repeat(" . . . . . . . ", $newData[$model]['depth']).$newData[$display['model']][$display['name']];
	
			unset($newData['children']);
			$result[] = $newData;
			if(isset($i['children']) && is_array($i['children'])) {
				$children = $this->treeComboBox($i['children'],$model,$display);
				foreach($children as $k) {
					$result[] = $k;
				}
			}
		}
		return $result;
	}
	
	
	public function articles(){
		// if selected menu
		if(array_key_exists($this->params->url, $this->links)){
			// get articles for selected menu
			(Array)$selected_menu = $this->links[$this->params->url];
			
			// set to view var articles
			$this->Articles->getArticles($selected_menu);
// 			$this->render(implode('/', array('menus')));
		}
	}
}
